﻿namespace BM.Tools.WebControls.MForm.Design
{
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Web.UI;
    using System.Web.UI.Design;
    using System.Web.UI.WebControls;

    using BM.Tools.WebControls.MForm.Controls;
    using BM.Tools.WebControls.MForm.Controls.DefaultTemplates;

    /// <summary>
    /// This is the <see cref="ControlDesigner"/> of the <see cref="Leaf"/> control.
    /// </summary>
    internal class LeafDesigner : ControlDesigner
    {        
        private DesignerActionListCollection actionLists;
        
        private TemplateGroupCollection templateGroups;

        /// <summary>
        /// Gets the action lists of the control.
        /// </summary>
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (null == actionLists)
                {
                    actionLists = new DesignerActionListCollection();
                    actionLists.Add(new OffspringActionList(this, Leaf));
                }

                return actionLists;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the designer can be resized.
        /// </summary>
        public override bool AllowResize
        {
            get
            {
                return InTemplateMode;
            }
        }

        /// <summary>
        /// Gets or sets LabelHolder.
        /// </summary>
        public ITemplate LabelHolder
        {
            get
            {
                return Leaf.LabelHolder ?? new DefaultLabelTemplate(Leaf);
            }

            set
            {
                if (!(value is DefaultLabelTemplate))
                {
                    SetTemplate("LabelHolder", value);
                }
            }
        }

        /// <summary>
        /// Gets the Leaf control.
        /// </summary>
        public Leaf Leaf
        {
            get
            {
                return ViewControl as Leaf;
            }
        }

        /// <summary>
        /// Gets TemplateGroups.
        /// </summary>
        public override TemplateGroupCollection TemplateGroups
        {
            get
            {
                if (templateGroups == null)
                {
                    templateGroups = base.TemplateGroups;
                    templateGroups.Add(GetValueHandlerTemplateGroup());
                }

                return templateGroups;
            }
        }

        /// <summary>
        /// Gets or sets the ValueHandler template.
        /// </summary>
        public ITemplate ValueHandler
        {
            get
            {
                ITemplate handler = Leaf.ValueHandler;
                if (handler == null)
                {
                    handler = new TextBoxValueHandlerTemplate();
                }

                return handler;
            }

            set
            {
                if (!(value is TextBoxValueHandlerTemplate))
                {
                    SetTemplate("ValueHandler", value);
                }
            }
        }

        /// <summary>
        /// Retrieves the HTML markup that is used to represent the control at design time.
        /// </summary>
        /// <returns>
        /// The HTML markup used to represent the control at design time.
        /// </returns>
        public override string GetDesignTimeHtml()
        {
            if (InTemplateMode)
            {
                return base.GetDesignTimeHtml();
            }

            return @"<div style=""width: 80%; margin: 2px; "">" + base.GetDesignTimeHtml() + "</div>";
        }

        /// <summary>
        /// Initializes the designer and sets template editing to true
        /// </summary>
        /// <param name="component">
        /// The component.
        /// </param>
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            SetViewFlags(ViewFlags.TemplateEditing, true);
        }

        private void SetTemplate(string propertyName, object value)
        {
            PropertyDescriptor property = TypeDescriptor.GetProperties(Leaf)[propertyName];
            property.SetValue(Leaf, value);
            OnComponentChanged(Component, new ComponentChangedEventArgs(Component, property, null, null));
            (Leaf as ICompositeControlDesignerAccessor).RecreateChildControls();
        }

        private TemplateGroup GetValueHandlerTemplateGroup()
        {
            TemplateGroup group = new TemplateGroup("Leaf templates");
            group.AddTemplateDefinition(new TemplateDefinition(this, "Label", this, "LabelHolder"));
            group.AddTemplateDefinition(new TemplateDefinition(this, "Value Handler", this, "ValueHandler", true));
            return group;
        }
    }
}